<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/yangshucheng2020/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/yangshucheng2020/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/yangshucheng2020/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/yangshucheng2020/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/yangshucheng2020/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/yangshucheng2020/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/yangshucheng2020/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="算法,C++基础知识,刷题必备," />










<meta name="description" content="头文件   头文件代码 用途    #include &lt;stdio.h&gt; 输入输出   #include  malloc()   #include &lt;string.h&gt; memset函数，strlen()等函数   #include     using namespace std; cin，cout，文件操作   #include  使用流操纵算子需要   #include">
<meta property="og:type" content="article">
<meta property="og:title" content="算法设计预备知识">
<meta property="og:url" content="http://yangshucheng2020.gitee.io/yangshucheng2020/2020/03/27/%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E9%A2%84%E5%A4%87%E7%9F%A5%E8%AF%86/index.html">
<meta property="og:site_name" content="羊书change">
<meta property="og:description" content="头文件   头文件代码 用途    #include &lt;stdio.h&gt; 输入输出   #include  malloc()   #include &lt;string.h&gt; memset函数，strlen()等函数   #include     using namespace std; cin，cout，文件操作   #include  使用流操纵算子需要   #include">
<meta property="article:published_time" content="2020-03-27T02:58:59.078Z">
<meta property="article:modified_time" content="2020-03-27T04:35:00.194Z">
<meta property="article:author" content="羊书change">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="C++基础知识">
<meta property="article:tag" content="刷题必备">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/yangshucheng2020/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yangshucheng2020.gitee.io/yangshucheng2020/2020/03/27/算法设计预备知识/"/>





  <title>算法设计预备知识 | 羊书change</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/yangshucheng2020/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">羊书change</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/yangshucheng2020/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/yangshucheng2020/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/yangshucheng2020/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/yangshucheng2020/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yangshucheng2020.gitee.io/yangshucheng2020/yangshucheng2020/2020/03/27/%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E9%A2%84%E5%A4%87%E7%9F%A5%E8%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="羊书change">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://gitee.com/yangshucheng2020/blogimage/raw/master/uPic/海绵jojo.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="羊书change">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">算法设计预备知识</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-27T10:58:59+08:00">
                2020-03-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/yangshucheng2020/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="头文件"><a href="#头文件" class="headerlink" title="头文件"></a>头文件</h2><table>
<thead>
<tr>
<th>头文件代码</th>
<th>用途</th>
</tr>
</thead>
<tbody><tr>
<td>#include &lt;stdio.h&gt;</td>
<td>输入输出</td>
</tr>
<tr>
<td>#include <cstdlib></td>
<td>malloc()</td>
</tr>
<tr>
<td>#include &lt;string.h&gt;</td>
<td>memset函数，strlen()等函数</td>
</tr>
<tr>
<td>#include <iostream>    using namespace std;</td>
<td>cin，cout，文件操作</td>
</tr>
<tr>
<td>#include <iomanip></td>
<td>使用流操纵算子需要</td>
</tr>
<tr>
<td>#include &lt;io.h&gt;</td>
<td>_access(char *,int)</td>
</tr>
<tr>
<td>#include &lt;stdbool.h&gt;</td>
<td>c语言也可以用bool类型</td>
</tr>
<tr>
<td>#include <algorithm></td>
<td>sort()方法,swap()交换函数,max(),min(),fill()</td>
</tr>
<tr>
<td>#include <string></td>
<td>string str,str.size(),find(),substr(),insert(),erase(),clear()</td>
</tr>
<tr>
<td>#include <cstring></td>
<td>memset()函数</td>
</tr>
<tr>
<td>#include <vector></td>
<td>vector<typename> name</td>
</tr>
<tr>
<td>#include <fstream>    using namespace std;</td>
<td>文件操作</td>
</tr>
<tr>
<td>#include &lt;ctype.h&gt;</td>
<td>isalpha()参数为字母字符返回非零值，否则返回零值</td>
</tr>
<tr>
<td>#include <cmath></td>
<td>三角函数、幂函数、取整、绝对值、随机数</td>
</tr>
<tr>
<td>#include &lt;bits/stdc++.h&gt;</td>
<td>包含几乎所有c++头文件</td>
</tr>
<tr>
<td>#include <climits></td>
<td>INT_MAX和INT_MIN的宏定义可直接使用3</td>
</tr>
</tbody></table>
<h2 id="scanf与printf"><a href="#scanf与printf" class="headerlink" title="scanf与printf"></a>scanf与printf</h2><h3 id="常见数据类型变量的scanf格式符"><a href="#常见数据类型变量的scanf格式符" class="headerlink" title="常见数据类型变量的scanf格式符"></a>常见数据类型变量的scanf格式符</h3><table>
<thead>
<tr>
<th>数据类型</th>
<th>格式符</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>int</td>
<td>%d</td>
<td>scanf(“%d”,&amp;n);</td>
</tr>
<tr>
<td>long long</td>
<td>%lld</td>
<td>scanf(“%lld”,&amp;n);</td>
</tr>
<tr>
<td>float</td>
<td>%f</td>
<td>scanf(“%f”,&amp;fl);</td>
</tr>
<tr>
<td><strong>double</strong></td>
<td><strong>%lf</strong></td>
<td><strong>scanf(“%lf”,&amp;db);</strong></td>
</tr>
<tr>
<td>char</td>
<td>%c</td>
<td>scanf(“%c”,&amp;c);</td>
</tr>
<tr>
<td>字符串（char数组）</td>
<td>%s</td>
<td>scanf(“%s”,str);</td>
</tr>
</tbody></table>
<ul>
<li>scanf(“格式控制”,变量地址)</li>
<li>在scanf中，除了char数组整个输入的情况不加&amp;之外，其他类型变量都需要加&amp;</li>
<li>数组名本身就代表了这个数组第一个元素的地址，所以不需要再加取地址运算符</li>
<li>scanf的双引号内的内容其实就是整个输入，只不过把数据换成它们对应的格式符并把变量的地址按次序写在后面而已</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> hh,mm,ss;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d:%d:%d"</span>,&amp;hh,&amp;mm,&amp;ss);</span><br><span class="line"><span class="comment">//输入如  hh:mm:ss</span></span><br></pre></td></tr></table></figure>

<p>另外，如果要输入“3 4”这种用空格隔开的两个数字，两个%d之间可以不用加空格</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a,b;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;a,&amp;b);</span><br></pre></td></tr></table></figure>

<p>可以不加空格的原因：除了<strong>%c</strong>外，scanf对其他格式符（如<strong>%d</strong>）的输入是以空白符（即空格、换行等）为结束判断标志的，因此除非使用%c把空格按字符读入，其他情况都会自动跳过空格</p>
<ul>
<li>字符数组使用%s读入的时候以 <strong>空格</strong> 跟 <strong>换行</strong> 为读入结束的标志</li>
<li>scanf的<strong>%c</strong>格式是可以读入空格跟换行的</li>
</ul>
<h3 id="常见的printf格式符"><a href="#常见的printf格式符" class="headerlink" title="常见的printf格式符"></a>常见的printf格式符</h3><table>
<thead>
<tr>
<th>数据类型</th>
<th>格式符</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>int</td>
<td>%d</td>
<td>printf(“%d”,n);</td>
</tr>
<tr>
<td>long long</td>
<td>%lld</td>
<td>printf(“%lld”,n);</td>
</tr>
<tr>
<td>float</td>
<td>%f</td>
<td>printf(“%f”,fl);</td>
</tr>
<tr>
<td><strong>double</strong></td>
<td><strong>%f</strong></td>
<td><strong>printf(“%f”,db);</strong></td>
</tr>
<tr>
<td>char</td>
<td>%c</td>
<td>printf(“%c”,c);</td>
</tr>
<tr>
<td>字符串（char数组）</td>
<td>%s</td>
<td>printf(“%s”,str);</td>
</tr>
</tbody></table>
<ul>
<li>对于double类型的变量，其<strong>输出格式变成了%f</strong>，而在scanf中却是%lf</li>
<li>如果想要输出%或者\，则需要在前面加一个%或者\</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">"%%"</span>);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"//"</span>);</span><br></pre></td></tr></table></figure>

<h4 id="三种实用的输出格式"><a href="#三种实用的输出格式" class="headerlink" title="三种实用的输出格式"></a>三种实用的输出格式</h4><ol>
<li><p>%md</p>
<p>%md可以使不足m位的int型变量<strong>以m位进行右对齐输出</strong>，其中高位用空格补齐，如果本身超过m位，则保持原样。</p>
</li>
<li><p>%0md</p>
<p>%0md只是在%md中间多加了0。和%md的唯一不同点在于，当变量不足m位时，将在前面补足够数量的0而不是空格。</p>
</li>
<li><p>%.mf</p>
<p>%.mf可以让浮点数保留m位小数输出，这个“保留”使用的是精度的“四舍六入五成双”，题目要求：保留xx位小数。四舍五入用round函数</p>
</li>
</ol>
<h2 id="使用getchar和putchar输入-输出字符"><a href="#使用getchar和putchar输入-输出字符" class="headerlink" title="使用getchar和putchar输入/输出字符"></a>使用getchar和putchar输入/输出字符</h2><p>getchar用来输入单个字符，putchar用来输出单个字符</p>
<p>getchar可以识别换行符</p>
<h2 id="getline-lt-字符数组chs-gt-，-lt-读取字符的个数n-gt-，-lt-终止符-gt"><a href="#getline-lt-字符数组chs-gt-，-lt-读取字符的个数n-gt-，-lt-终止符-gt" class="headerlink" title="getline(&lt;字符数组chs&gt;，&lt;读取字符的个数n&gt;，&lt;终止符&gt;)"></a>getline(&lt;字符数组chs&gt;，&lt;读取字符的个数n&gt;，&lt;终止符&gt;)</h2><ul>
<li>此函数可读取整行，<strong>包括前导和嵌入的空格</strong>，并将其存储在字符串对象中。</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// This program illustrates using the getline function</span></span><br><span class="line"><span class="comment">//to read character data into a string object.</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt; // Header file needed to use string objects</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">string</span> name;</span><br><span class="line">    <span class="built_in">string</span> city;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Please enter your name: "</span>;</span><br><span class="line">    getline(<span class="built_in">cin</span>, name);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Enter the city you live in: "</span>;</span><br><span class="line">    getline(<span class="built_in">cin</span>, city);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Hello, "</span> &lt;&lt; name &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"You live in "</span> &lt;&lt; city &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>程序输出结果：</p>
<blockquote>
<p>Please enter your name: John Doe<br> Enter the city you live in: Chicago<br> Hello, John Doe<br> You live in Chicago</p>
</blockquote>
<h2 id="gets输入与puts输出"><a href="#gets输入与puts输出" class="headerlink" title="gets输入与puts输出"></a>gets输入与puts输出</h2><h4 id="gets"><a href="#gets" class="headerlink" title="gets"></a>gets</h4><ul>
<li><p>gets用来输入一行字符串</p>
</li>
<li><p>gets识别换行符\n作为输入结束，因此scanf完一个整数后，如果要使用gets，需先用getchar接收整数后的换行符，并将其存放于一维数组（或二维数组的一维）中</p>
</li>
<li><p>puts用来输出一行字符串，即将一维数组（或二维数组的一维）在界面上输出，并紧跟一个换行</p>
</li>
</ul>
<h2 id="输入字符串特别提醒"><a href="#输入字符串特别提醒" class="headerlink" title="输入字符串特别提醒"></a>输入字符串特别提醒</h2><h4 id="结束符-0的ASCII码为0，即空字符NULL，占用一个字符位，因此开辟数组时必须保证字符数组的长度一定要比实际存储字符串的长度至少多1"><a href="#结束符-0的ASCII码为0，即空字符NULL，占用一个字符位，因此开辟数组时必须保证字符数组的长度一定要比实际存储字符串的长度至少多1" class="headerlink" title="结束符\0的ASCII码为0，即空字符NULL，占用一个字符位，因此开辟数组时必须保证字符数组的长度一定要比实际存储字符串的长度至少多1"></a>结束符\0的ASCII码为0，即空字符NULL，占用一个字符位，因此开辟数组时必须保证字符数组的长度一定要比实际存储字符串的长度至少多1</h4><h4 id="如果不是使用scanf函数的-s格式或gets函数输入字符串（例如使用getchar），一定要在输入的每个字符串后面加入“-0”"><a href="#如果不是使用scanf函数的-s格式或gets函数输入字符串（例如使用getchar），一定要在输入的每个字符串后面加入“-0”" class="headerlink" title="如果不是使用scanf函数的%s格式或gets函数输入字符串（例如使用getchar），一定要在输入的每个字符串后面加入“\0”"></a>如果不是使用scanf函数的%s格式或gets函数输入字符串（例如使用getchar），一定要在输入的每个字符串后面加入“\0”</h4><h2 id="常用math函数"><a href="#常用math函数" class="headerlink" title="常用math函数"></a>常用math函数</h2><h3 id="fabs-double-x"><a href="#fabs-double-x" class="headerlink" title="fabs(double x)"></a>fabs(double x)</h3><p>对double型变量取绝对值</p>
<h3 id="floor-double-x-和ceil-double-x"><a href="#floor-double-x-和ceil-double-x" class="headerlink" title="floor(double x)和ceil(double x)"></a>floor(double x)和ceil(double x)</h3><p>floor向下取整，ceil向上取整，返回类型为double型</p>
<h3 id="pow-double-r-double-p"><a href="#pow-double-r-double-p" class="headerlink" title="pow(double r,double p)"></a>pow(double r,double p)</h3><p>该函数返回<br>$$<br>r^p<br>$$<br>要求r和p都是double型</p>
<h3 id="sqrt-double-x"><a href="#sqrt-double-x" class="headerlink" title="sqrt(double x)"></a>sqrt(double x)</h3><p>返回double型变量的算术平方根</p>
<h3 id="log-double-x"><a href="#log-double-x" class="headerlink" title="log(double x)"></a>log(double x)</h3><p>返回double型变量的<strong>以自然对数为底</strong>的对数</p>
<p><strong>C语言中没有对任意底数求对数的函数，因此必须使用换底公式</strong></p>
<h3 id="sin-double-x-、cos-double-x-、tan-double-x"><a href="#sin-double-x-、cos-double-x-、tan-double-x" class="headerlink" title="sin(double x)、cos(double x)、tan(double x)"></a>sin(double x)、cos(double x)、tan(double x)</h3><p>参数要求是弧度制</p>
<h3 id="asin-double-x-、acos-double-x-、atan-double-x"><a href="#asin-double-x-、acos-double-x-、atan-double-x" class="headerlink" title="asin(double x)、acos(double x)、atan(double x)"></a>asin(double x)、acos(double x)、atan(double x)</h3><h3 id="round-double-x"><a href="#round-double-x" class="headerlink" title="round(double x)"></a>round(double x)</h3><p>将double型变量x四舍五入，返回类型也是double型，需进行取整</p>
<h2 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h2><p>\n代表换行</p>
<p>\0代表空字符NULL，其ASCII码为0</p>
<h2 id="符号常量和const常量"><a href="#符号常量和const常量" class="headerlink" title="符号常量和const常量"></a>符号常量和const常量</h2><h4 id="符号常量："><a href="#符号常量：" class="headerlink" title="符号常量："></a>符号常量：</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> 标识符 常量</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pi 3.14</span></span><br></pre></td></tr></table></figure>

<p>末尾不加分号</p>
<h4 id="const常量"><a href="#const常量" class="headerlink" title="const常量"></a>const常量</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> 数据类型 变量名 = 常量;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> pi = <span class="number">3.14</span>;</span><br></pre></td></tr></table></figure>

<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h4><p>define除了可定义常量外，还可以定义任何语句或者片段</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> 标识符  任何语句或者片段</span></span><br></pre></td></tr></table></figure>

<p>宏定义是直接将对应的部分替换，然后才进行编译和运行</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> CAL(x) (x * 2 + 1)</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> a = <span class="number">1</span>;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,CAL(a + <span class="number">1</span>));</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果：</p>
<blockquote>
<p>4</p>
</blockquote>
<p>它把替换的部分原封不动替换进去</p>
<p>导致CAL(a + 1)实际上是（a + 1*2 +1）也就是1 + 2 + 1 = 4</p>
<h2 id="typedef"><a href="#typedef" class="headerlink" title="typedef"></a>typedef</h2><p><strong>用途1：</strong></p>
<p>定义一种类型的别名，而不只是简单的宏替换。可以用作同时声明指针型的多个对象。比如：<br> char* pa, pb; // 这多数不符合我们的意图，它只声明了一个指向字符变量的指针，<br> // 和一个字符变量；<br> 以下则可行：<br> typedef char* PCHAR; // 一般用大写<br> PCHAR pa, pb; // 可行，同时声明了两个指向字符变量的指针<br> 虽然：<br> char *pa, *pb;<br> 也可行，但相对来说没有用typedef的形式直观，尤其在需要大量指针的地方，typedef的方式更省事。</p>
<p><strong>用途二：</strong></p>
<p>用在旧的C的代码中（具体多旧没有查），帮助struct。以前的代码中，声明struct新对象时，必须要带上struct，即形式为： struct 结构名 对象名，如：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tagPOINT1</span>  </span></span><br><span class="line"><span class="class">&#123;</span>  </span><br><span class="line">    <span class="keyword">int</span> x;  </span><br><span class="line">    <span class="keyword">int</span> y;  </span><br><span class="line">&#125;;  </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tagPOINT1</span> <span class="title">p1</span>;</span></span><br></pre></td></tr></table></figure>

<p>而在C++中，则可以直接写：结构名 对象名，即：</p>
<p>tagPOINT1 p1;</p>
<p>老多写struct太麻烦了，于是就发明了：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">tagPOINT</span>  </span></span><br><span class="line"><span class="class">&#123;</span>  </span><br><span class="line">    <span class="keyword">int</span> x;  </span><br><span class="line">    <span class="keyword">int</span> y;  </span><br><span class="line">&#125;POINT;  </span><br><span class="line">POINT p1; <span class="comment">// 这样就比原来的方式少写了一个struct，比较省事，尤其在大量使用的时候</span></span><br></pre></td></tr></table></figure>

<p>或许，在C++中，typedef的这种用途二不是很大，但是理解了它，对掌握以前的旧代码还是有帮助的，毕竟我们在项目中有可能会遇到较早些年代遗留下来的代码。</p>
<p><strong>用途三：</strong></p>
<p>用typedef来定义与平台无关的类型。<br> 比如定义一个叫 REAL 的浮点类型，在目标平台一上，让它表示最高精度的类型为：<br> typedef long double REAL;<br> 在不支持 long double 的平台二上，改为：<br> typedef double REAL;<br> 在连 double 都不支持的平台三上，改为：<br> typedef float REAL;<br> 也就是说，当跨平台时，只要改下 typedef 本身就行，不用对其他源码做任何修改。<br> 标准库就广泛使用了这个技巧，比如size_t。<br> 另外，因为typedef是定义了一种类型的新别名，不是简单的字符串替换，所以它比宏来得稳健（虽然用宏有时也可以完成以上的用途）。</p>
<h2 id="memeset——对数组中每一个元素赋相同的值"><a href="#memeset——对数组中每一个元素赋相同的值" class="headerlink" title="memeset——对数组中每一个元素赋相同的值"></a>memeset——对数组中每一个元素赋相同的值</h2><p>memset格式：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="built_in">memset</span>(数组名,值,<span class="keyword">sizeof</span>(数组名))；</span><br><span class="line"><span class="comment">//建议对memset赋0或者-1</span></span><br></pre></td></tr></table></figure>

<ul>
<li>如果要对数组赋其他数字，那么使用fill函数</li>
</ul>
<h2 id="string-h头文件"><a href="#string-h头文件" class="headerlink" title="string.h头文件"></a>string.h头文件</h2><h3 id="strlen"><a href="#strlen" class="headerlink" title="strlen()"></a>strlen()</h3><p>strlen函数可以得到字符数组中第一个\0前的字符的个数</p>
<h3 id="strcmp"><a href="#strcmp" class="headerlink" title="strcmp()"></a>strcmp()</h3><p>strcmp函数返回两个字符串大小的比较结果，比较原则是按 <strong>字典序</strong>（即在字典中的顺序，后面的更大）</p>
<p>“a” 小于 “b”</p>
<p>“aaaa”小于“aab”</p>
<p>strcmp返回结果如下：</p>
<p>①如果字符数组1 &lt; 字符数组2，则返回一个负整数</p>
<p>②如果字符数组1 == 字符数组2，则返回0</p>
<p>③字符数组1 &gt; 字符数组2，则返回一个正整数</p>
<h3 id="strcpy"><a href="#strcpy" class="headerlink" title="strcpy()"></a>strcpy()</h3><p><strong>strcpy()</strong>函数可以把一个字符串<strong>复制</strong>给另一个字符串</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">strcpy</span>(字符数组<span class="number">1</span>，字符数组<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<p>注意：是把<strong>字符数组2复制给字符数组1</strong>，包括结束符\0</p>
<h3 id="strcat"><a href="#strcat" class="headerlink" title="strcat()"></a>strcat()</h3><p><strong>strcat()</strong>函数可以把一个字符串<strong>接</strong>给另一个字符串</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">strcat</span>(字符数组<span class="number">1</span>，字符数组<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<p>注意：是把<strong>字符数组2接到字符数组1</strong>后面</p>
<h2 id="字符串-头文件"><a href="#字符串-头文件" class="headerlink" title="字符串 头文件"></a>字符串 <string>头文件</h2><ol>
<li><p>string的定义：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">string</span> str;</span><br></pre></td></tr></table></figure>
</li>
<li><p>string的初始化</p>
<p>直接给字符串变量赋值，如：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">string</span> str = <span class="string">"hello world"</span>;</span><br><span class="line"><span class="function"><span class="built_in">string</span> <span class="title">answer</span><span class="params">(str.<span class="built_in">size</span>(),<span class="string">'x'</span>)</span></span>;<span class="comment">//str长度个x</span></span><br><span class="line"><span class="built_in">cout</span>&lt;&lt;str&lt;&lt;<span class="built_in">endl</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>string的长度</p>
<p>返回当前的字符串长度的函数有size()和length():  str.length()    str.size()</p>
</li>
<li><p>string元素的访问</p>
<ol>
<li><p>可以像数组那样通过元素下标进行访问，下标从0到size()-1</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">string</span> str = <span class="string">"hello world"</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; str.<span class="built_in">size</span>();++i)&#123;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"%c"</span>str[i]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%c\n"</span>,str[<span class="number">6</span>]);</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<blockquote>
<p>hello world</p>
<p>w</p>
</blockquote>
</li>
<li><p>可以通过迭代器进行访问，迭代器类似于指针</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">string</span> str = <span class="string">"hello world"</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="built_in">string</span>::iterator it = str.<span class="built_in">begin</span>();it != str.<span class="built_in">end</span>();++it)&#123;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"%c"</span>,*it);</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>hello world</p>
</blockquote>
</li>
</ol>
</li>
<li><p>string中的元素操作</p>
<ol>
<li>在任意位置插入元素的insert()</li>
<li>在任意位置删除元素的erase()</li>
<li>将字符串清空的clear()</li>
</ol>
</li>
<li><p>string的运算符</p>
<ol>
<li>“+”“+=”连接字符串和字符串或字符的运算符</li>
<li>按照字典序进行大小比较，比较运算符有： &lt; 、&gt;、&lt;=、&gt;=,判断两个字符串是否相等的运算符有==以及！=</li>
</ol>
</li>
<li><p>string常用的函数</p>
<ol>
<li>在字符串中寻找特定字符或字符串的函数是find()。若函数找到相应的字符或者字符串则返回对应的下标，若找不到返回string::npos</li>
<li>返回字符串的子串的函数是substr(子串开始坐标，从开始坐标后的n个字符)</li>
</ol>
</li>
</ol>
<h2 id="cin与cout"><a href="#cin与cout" class="headerlink" title="cin与cout"></a>cin与cout</h2><p>头文件 ：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iomanip&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> n;</span><br><span class="line">  <span class="built_in">cin</span>&gt;&gt;n;</span><br><span class="line">  <span class="comment">//输入一整行</span></span><br><span class="line">  <span class="keyword">char</span> str[<span class="number">100</span>];</span><br><span class="line">  <span class="built_in">cin</span>.getline(str,<span class="number">100</span>);</span><br><span class="line">  <span class="comment">/************************************/</span></span><br><span class="line">  <span class="comment">//输出 cout</span></span><br><span class="line">  <span class="built_in">cout</span>&lt;&lt;n;</span><br><span class="line">  <span class="built_in">cout</span>&lt;&lt;n&lt;&lt;<span class="string">"\n"</span>;</span><br><span class="line">  <span class="built_in">cout</span>&lt;&lt;<span class="built_in">endl</span>;<span class="comment">//换行可以用 “\n” 或者 endl</span></span><br><span class="line">  <span class="comment">//若想控制double型的精度，例如输出小数点后两位，需加上#include &lt;iomanip&gt; 文件</span></span><br><span class="line">  <span class="built_in">cout</span>&lt;&lt;setiosflags(ios::fixed)&lt;&lt;setprecision(<span class="number">2</span>)&lt;&lt;<span class="number">123.4567</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">  <span class="comment">//输出结果  123.46</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//若使用string容器</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="built_in">string</span> str;</span><br><span class="line">getline(<span class="built_in">cin</span>,str);</span><br></pre></td></tr></table></figure>

<h3 id="流操纵算子"><a href="#流操纵算子" class="headerlink" title="流操纵算子"></a>流操纵算子</h3><p>• 整数流的基数：流操纵算子dec,oct,hex,setbase<br>• 浮点数的精度（precision,setprecision）</p>
<p>precision, setprecision<br> precision是成员函数，其调用方式为：<br>cout.precision(5);<br> setprecision 是流操作算子，其调用方式为：<br>cout &lt;&lt; setprecision(5); // 可以连续输出<br>它们的功能相同。<br>指定输出浮点数的有效位数（非定点方式输出时）<br>指定输出浮点数的小数点后的有效位数（定点方式输出时）<br>定点方式：小数点必须出现在个位数后面</p>
<p>• 设置域宽(setw,width)<br>• 用户自定义的流操纵算子</p>
<h2 id="指针变量"><a href="#指针变量" class="headerlink" title="指针变量"></a>指针变量</h2><p><strong>指针变量用来存放指针（或者可以理解为地址）</strong></p>
<p>指针变量存放的是地址，而&amp;则是取地址运算符，因此给指针变量赋值的方式一般是把变量的地址取出来，然后赋给对应类型的指针变量</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a;</span><br><span class="line"><span class="keyword">int</span> *p = &amp;a;</span><br><span class="line"><span class="comment">/******************/</span></span><br><span class="line"><span class="keyword">int</span> *q;</span><br><span class="line">q = &amp;a;</span><br><span class="line"><span class="comment">/*******************/</span></span><br><span class="line"><span class="keyword">int</span> a,b;</span><br><span class="line"><span class="keyword">int</span> *p1 = &amp;a,*p2 = &amp;b;</span><br></pre></td></tr></table></figure>

<p>int* 是指针变量的类型，而后面的p才是变量名，用来存储地址，因此地址&amp;a是赋值给p而不是*p</p>
<ul>
<li>对于一个int*型的指针变量p来说，p+1是指p所指的int型变量的下一个int型变量地址</li>
<li>指针变量支持自增和自减操作</li>
<li>对指针变量来说，把其存储的地址的类型称为基类型</li>
</ul>
<h3 id="指针与数组"><a href="#指针与数组" class="headerlink" title="指针与数组"></a>指针与数组</h3><p><strong>数组名称</strong>也作为<strong>数组的首地址</strong>使用</p>
<p>a + i 等同于&amp;a[i]</p>
<p>两个int型的指针相减，等价于在求两个指针之间相差了几个int</p>
<h3 id="使用指针变量作为函数参数"><a href="#使用指针变量作为函数参数" class="headerlink" title="使用指针变量作为函数参数"></a>使用指针变量作为函数参数</h3><p>指针类型也可以作为函数参数的类型，这时视为把变量的地址传入函数。如果在函数中对这个地址中的元素进行改变，原先的数据就会确实的被改变。</p>
<p>只有在获取地址的情况下对元素进行操作，才能真正地修改变量。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">//正确写法：</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span>* a, <span class="keyword">int</span>* b)</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> temp = *a;</span><br><span class="line">  *a = *b;</span><br><span class="line">  *b = temp;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//错误写法：定义int*型的指针变量temp时，temp没有被初始化，也就是说，指针变量temp中存放的地址是随机的，如果该随机地址指向的是系统工作区间，那么就会出错（很有可能）</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">void swap(int* a, int* b)&#123;</span></span><br><span class="line"><span class="comment">  int temp = *a;</span></span><br><span class="line"><span class="comment">  *a = *b;</span></span><br><span class="line"><span class="comment">  *b = *temp;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">修改为正确的写法：</span></span><br><span class="line"><span class="comment">void swap(int* a, int* b)&#123;</span></span><br><span class="line"><span class="comment">	int x;</span></span><br><span class="line"><span class="comment">  int temp = &amp;x;</span></span><br><span class="line"><span class="comment">  *temp = *a;</span></span><br><span class="line"><span class="comment">  *a = *b;</span></span><br><span class="line"><span class="comment">  *b = *temp;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">//错误写法二：直接把地址交换，在main中调用的时候还是副本传递，没法对数据产生实质性的影响，即相当于把int*看作一个整体，传入的a和b都只是地址的副本。</span></span><br><span class="line"><span class="comment">//函数参数的传送方式是单向一次性的，main函数传给swap函数的“地址”其实是一个“无符号整型”的数，其本身也跟普通变量一样只是“值传递”，swap函数对地址本身进行修改并不能对main函数里的地址修改，能够使main函数里的数据放生变化的方式只能是   swap函数中对地址指向的数据进行的修改   对地址本身进行修改其实跟之前对传入的普通变量进行交换的函数是一样的作用，都只是副本</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">void swap(int* a, int* b)&#123;</span></span><br><span class="line"><span class="comment">  int* temp = a;</span></span><br><span class="line"><span class="comment">  a = b;</span></span><br><span class="line"><span class="comment">  b = temp;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> a = <span class="number">1</span>,b = <span class="number">2</span>;</span><br><span class="line">  <span class="keyword">int</span> *p1 = &amp;a,*p2 = &amp;b;</span><br><span class="line">  swap(p1,p2);</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"a = %d,b = %d\n"</span>,*p1,*p2);</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h2><ul>
<li>引用不产生副本，而是给原变量起了个别名，且对引用变量的操作就是对原变量的操作</li>
<li>引用的方法：只需要在函数的参数类型后面加上个&amp;就可以了</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">change</span><span class="params">(<span class="keyword">int</span> &amp;x)</span></span>&#123;</span><br><span class="line">  x = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line">  change(a);</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,a);</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//输出结果：  1</span></span><br></pre></td></tr></table></figure>

<p>在change函数的参数int x中加了&amp;，在传入参数时对参数的修改就会对原变量进行修改，不管是否使用引用，函数的参数名和实际传入的参数名可以不同，引用不是取地址的意思。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">//把int*型理解成unsigned int 型，而直接交换这样的两个整型变量是需要加引用的</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span>* &amp;a, <span class="keyword">int</span>* &amp;b)</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> *temp = a;</span><br><span class="line">  a = b;</span><br><span class="line">  b = temp;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> a = <span class="number">1</span>,b = <span class="number">2</span>;</span><br><span class="line">  <span class="keyword">int</span> *p1 = &amp;a,*p2 = &amp;b;</span><br><span class="line">  swap(p1,p2);</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"a = %d,b = %d\n"</span>,*p1,*p2);</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>由于引用是产生变量的别名，因此常量不可以使用引用。于是上面的代码不可以写成swap(&amp;a,&amp;b),而必须用指针变量p1和p2存放&amp;a和&amp;b，然后把指针变量作为参数传入。</p>
<h2 id="结构体"><a href="#结构体" class="headerlink" title="结构体"></a>结构体</h2><p>定义一个结构体：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Name</span>&#123;</span></span><br><span class="line">  <span class="comment">//一些基本的数据结构或者自定义的数据结构</span></span><br><span class="line">&#125;<span class="comment">//别名;</span></span><br></pre></td></tr></table></figure>

<p>结构体里面能定义除了自己本身之外的任何数据类型。不过虽然不能定义自己本身，但可以定义自身类型的指针变量。如：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>&#123;</span></span><br><span class="line">  <span class="comment">//node n;//不能定义node型变量</span></span><br><span class="line">  node* next;<span class="comment">//可以定义node*型指针变量</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="访问结构体内的元素"><a href="#访问结构体内的元素" class="headerlink" title="访问结构体内的元素"></a>访问结构体内的元素</h3><p>“.”操作和“-&gt;”操作</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">student</span>&#123;</span></span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">  <span class="keyword">char</span> name[<span class="number">20</span>];</span><br><span class="line">  student* next;</span><br><span class="line">&#125;stu,*p;</span><br><span class="line"></span><br><span class="line"><span class="comment">//访问普通变量：</span></span><br><span class="line">stu.id;</span><br><span class="line"><span class="comment">//访问指针变量</span></span><br><span class="line">(*p).id;</span><br><span class="line">p-&gt;id;</span><br></pre></td></tr></table></figure>

<h3 id="结构体的构造函数初始化"><a href="#结构体的构造函数初始化" class="headerlink" title="结构体的构造函数初始化"></a>结构体的构造函数初始化</h3><p>构造函数是用来初始化结构体的一种函数，它直接定义在结构体中，它的特点是它不需要写返回类型，且函数名与结构体相同，默认构造函数里面什么都没有如：student(){}，由于它的存在，才可以直接定义student类型的变量而不进行初始化。</p>
<p>自己提供构造函数的初始化参数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">studentInfo</span>&#123;</span></span><br><span class="line">  <span class="keyword">int</span> id;</span><br><span class="line">  <span class="keyword">char</span> gender;</span><br><span class="line">  studentInfo(<span class="keyword">int</span> _id,<span class="keyword">char</span> _gender):id(_id),gender(_gender)&#123;&#125;</span><br><span class="line">  <span class="comment">//等价于</span></span><br><span class="line">  studentInfo(<span class="keyword">int</span> _id,<span class="keyword">char</span> _gender)&#123;</span><br><span class="line">    id = _id;</span><br><span class="line">    gender = _gender);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//使用构造函数初始化：</span></span><br><span class="line">studentInfo stu = studentInfo(<span class="number">10086</span>,<span class="string">'M'</span>);</span><br></pre></td></tr></table></figure>

<p>如果自己<strong>重新定义了构造函数，则必须经初始化就定义结构体变量</strong></p>
<p>为了既能不初始化就定义结构体变量，又能享受初始化带来的便捷，可以把“studentInfo(){}”手动加上。这意味着，只要参数个数和类型不完全相同，就可以定义任意多个构造函数，以适应不同的初始化场合。</p>
<h2 id="浮点数的比较"><a href="#浮点数的比较" class="headerlink" title="浮点数的比较"></a>浮点数的比较</h2><p>浮点数并不总是精确的，需要引入一个极小数eps来对这种误差进行修正</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-8</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Equ(a,b)((fabs(a)-(b))&lt;(eps))<span class="comment">//等于判断</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> More(a,b)(((a)-(b))&gt;(eps))<span class="comment">//大于判断</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Less(a,b)(((a)-(b))&lt;(eps))<span class="comment">//小于判断</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MoreEqu(a,b)(((a)-(b))&gt;(-eps))<span class="comment">//大于等于判断</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LessEqu(a,b)(((a)-(b))&lt;(eps))<span class="comment">//小于等于判断</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> Pi = <span class="built_in">acos</span>(<span class="number">-1.0</span>);<span class="comment">//圆周率π</span></span><br></pre></td></tr></table></figure>

<h2 id="STL介绍"><a href="#STL介绍" class="headerlink" title="STL介绍"></a>STL介绍</h2><h3 id="vector"><a href="#vector" class="headerlink" title="vector"></a>vector</h3><h4 id="vector的定义"><a href="#vector的定义" class="headerlink" title="vector的定义"></a>vector的定义</h4><p>单独定义一个vector，可以看做 变长数组</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">typename</span>&gt; name;</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; name;</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">double</span>&gt; name;</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">char</span>&gt; name;</span><br><span class="line"><span class="built_in">vector</span>&lt;node&gt; name;<span class="comment">//node是结构体类型</span></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &gt; name;<span class="comment">//&gt;&gt;之间要加空格</span></span><br><span class="line"><span class="comment">//定义数组：</span></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">typename</span>&gt; Arrayname[arraySize];</span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; vi[<span class="number">100</span>];<span class="comment">//一维长度已经固定，另一维长度“变长”</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; vec;        <span class="comment">//声明一个int型向量</span></span><br><span class="line"><span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">vec</span><span class="params">(<span class="number">5</span>)</span></span>;     <span class="comment">//声明一个初始大小为5的int向量</span></span><br><span class="line"><span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">vec</span><span class="params">(<span class="number">10</span>, <span class="number">1</span>)</span></span>; <span class="comment">//声明一个初始大小为10且值都是1的向量</span></span><br><span class="line"><span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">vec</span><span class="params">(tmp)</span></span>;   <span class="comment">//声明并用tmp向量初始化vec向量</span></span><br><span class="line"><span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">tmp</span><span class="params">(vec.<span class="built_in">begin</span>(), vec.<span class="built_in">begin</span>() + <span class="number">3</span>)</span></span>;  <span class="comment">//用向量vec的第0个到第2个值初始化tmp</span></span><br><span class="line"><span class="keyword">int</span> arr[<span class="number">5</span>] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;   </span><br><span class="line"><span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">vec</span><span class="params">(arr, arr + <span class="number">5</span>)</span></span>;      <span class="comment">//将arr数组的元素用于初始化vec向量</span></span><br><span class="line"><span class="comment">//说明：当然不包括arr[4]元素，末尾指针都是指结束元素的下一个元素，</span></span><br><span class="line"><span class="comment">//这个主要是为了和vec.end()指针统一。</span></span><br><span class="line"><span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">vec</span><span class="params">(&amp;arr[<span class="number">1</span>], &amp;arr[<span class="number">4</span>])</span></span>; <span class="comment">//将arr[1]~arr[4]范围内的元素作为vec的初始值</span></span><br></pre></td></tr></table></figure>

<h4 id="vector的状态"><a href="#vector的状态" class="headerlink" title="vector的状态"></a>vector的状态</h4><p>empty():返回当前向量是否为空</p>
<p>size():返回当前向量元素个数</p>
<h4 id="vector尾部元素的添加或删除"><a href="#vector尾部元素的添加或删除" class="headerlink" title="vector尾部元素的添加或删除"></a>vector尾部元素的添加或删除</h4><p>push_back():向其尾部添加新元素</p>
<p>pop_back():删除尾部元素</p>
<h4 id="vector元素的访问"><a href="#vector元素的访问" class="headerlink" title="vector元素的访问"></a>vector元素的访问</h4><p>①可以像数组一般通过元素下标进行访问，下标从0到size()-1</p>
<p>②可以通过迭代器进行访问，迭代器类似于指针</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;::iterator it;<span class="comment">//迭代器</span></span><br><span class="line"><span class="keyword">for</span> (it = vec.<span class="built_in">begin</span>(); it != vec.<span class="built_in">end</span>(); it++)</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; *it &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">size_t</span> i = <span class="number">0</span>; i &lt; vec.<span class="built_in">size</span>(); i++) &#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; vec.at(i) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="vector元素的操作"><a href="#vector元素的操作" class="headerlink" title="vector元素的操作"></a>vector元素的操作</h4><p>insert():在任意位置插入元素</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">myVector.insert(myVector.<span class="built_in">begin</span>(),<span class="number">3</span>,<span class="number">15</span>);<span class="comment">//头部插入3个15</span></span><br><span class="line">myVector.erase(myVector.<span class="built_in">begin</span>() + <span class="number">5</span>,myVector.<span class="built_in">end</span>());<span class="comment">//删除第5后续的元素</span></span><br></pre></td></tr></table></figure>

<p>erase():在任意位置删除元素</p>
<p>clear():将向量清空</p>
<h4 id="vector迭代器操作"><a href="#vector迭代器操作" class="headerlink" title="vector迭代器操作"></a>vector迭代器操作</h4><p>begin():返回向量中的首元素的迭代器</p>
<p>end():返回向量中尾元素后一个位置的迭代器end()</p>
<h3 id="map"><a href="#map" class="headerlink" title="map"></a>map</h3><h4 id="map的定义"><a href="#map的定义" class="headerlink" title="map的定义"></a>map的定义</h4><p>map是映射</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">map</span>&lt;<span class="keyword">typename</span> T1,<span class="keyword">typename</span> T2&gt;  name;</span><br><span class="line"><span class="keyword">typename</span> T1 是映射关键字的类型</span><br><span class="line"><span class="keyword">typename</span> T2	是映射值的类型</span><br><span class="line">name 是映射的名字</span><br></pre></td></tr></table></figure>

<h4 id="map的状态"><a href="#map的状态" class="headerlink" title="map的状态"></a>map的状态</h4><p>empty():返回当前映射是否为空</p>
<p>size():返回当前映射元素个数</p>
<h4 id="map的添加与删除"><a href="#map的添加与删除" class="headerlink" title="map的添加与删除"></a>map的添加与删除</h4><ol>
<li>由于map内部重载了[]运算符，因此可以通过[key]的方式进行访问</li>
<li>可以使用at()进行访问</li>
<li>可以通过迭代器进行访问</li>
</ol>
<h4 id="map元素操作"><a href="#map元素操作" class="headerlink" title="map元素操作"></a>map元素操作</h4><p>find():查找特定元素的函数，若找到则返回该元素的迭代器，未找到则返回迭代器end()</p>
<p>clear():将映射清空</p>
<h4 id="map迭代器操作"><a href="#map迭代器操作" class="headerlink" title="map迭代器操作"></a>map迭代器操作</h4><p>begin():返回映射中首元素</p>
<p>end():返回映射尾元素之后一个位置</p>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="built_in">map</span>&lt;<span class="built_in">string</span>,<span class="keyword">int</span>&gt; myMap;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    myMap[<span class="string">"Emma"</span>] = <span class="number">67</span>;</span><br><span class="line">    myMap[<span class="string">"Benedict"</span>] = <span class="number">100</span>;</span><br><span class="line">    myMap.insert(pair&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt;(<span class="string">"Bob"</span>,<span class="number">72</span>));</span><br><span class="line">    myMap.insert(pair&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt;(<span class="string">"Mary"</span>,<span class="number">85</span>));</span><br><span class="line">    myMap.insert(pair&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt;(<span class="string">"Alice"</span>,<span class="number">93</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"the size of myMap:%d\n"</span>,myMap.<span class="built_in">size</span>());<span class="comment">//the size of myMap:5</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"the score of Benedict: %d\n"</span>,myMap[<span class="string">"Benedict"</span>]);<span class="comment">//the score of Benedict: 100</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"the score of Alice: %d\n"</span>,myMap.at(<span class="string">"Mary"</span>));<span class="comment">//the score of Alice: 85</span></span><br><span class="line">    myMap.erase(<span class="string">"Benecict"</span>);</span><br><span class="line">    <span class="built_in">map</span>&lt;<span class="built_in">string</span>,<span class="keyword">int</span>&gt;::iterator it;</span><br><span class="line">    <span class="keyword">for</span> (it = myMap.<span class="built_in">begin</span>(); it != myMap.<span class="built_in">end</span>(); it++) &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"the score of "</span> &lt;&lt; it-&gt;first;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">": "</span> &lt;&lt; it-&gt;second &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*the score of Alice: 93</span></span><br><span class="line"><span class="comment">     the score of Benedict: 100</span></span><br><span class="line"><span class="comment">     the score of Bob: 72</span></span><br><span class="line"><span class="comment">     the score of Emma: 67</span></span><br><span class="line"><span class="comment">     the score of Mary: 85*/</span></span><br><span class="line">    myMap.<span class="built_in">clear</span>();</span><br><span class="line">    <span class="keyword">if</span> (myMap.empty()) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"myMap is empty\n"</span>);<span class="comment">//myMap is empty</span></span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"myMap is not empty\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    it = myMap.<span class="built_in">find</span>(<span class="string">"Bob"</span>);</span><br><span class="line">    <span class="keyword">if</span> (it != myMap.<span class="built_in">end</span>()) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"Bob is found\n"</span>);<span class="comment">//Bob is not found</span></span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"Bob is not found\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"the size of myMap:%d\n"</span>,myMap.<span class="built_in">size</span>());<span class="comment">//the size of myMap:0</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h3><h4 id="queue的定义"><a href="#queue的定义" class="headerlink" title="queue的定义"></a>queue的定义</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">queue</span>&lt;<span class="keyword">typename</span>&gt; name;</span><br></pre></td></tr></table></figure>

<h4 id="queue的状态"><a href="#queue的状态" class="headerlink" title="queue的状态"></a>queue的状态</h4><p>empty():返回当前向量是否为空</p>
<p>size():返回当前向量元素个数</p>
<h4 id="queue尾部元素的添加或删除"><a href="#queue尾部元素的添加或删除" class="headerlink" title="queue尾部元素的添加或删除"></a>queue尾部元素的添加或删除</h4><p>push():向其队头添加新元素</p>
<p>pop():删除队尾元素</p>
<h4 id="queue的访问"><a href="#queue的访问" class="headerlink" title="queue的访问"></a>queue的访问</h4><p>只能对队列的头尾两端进行操作，获得头尾两端元素的函数分别是front()和back()</p>
<h3 id="优先队列"><a href="#优先队列" class="headerlink" title="优先队列"></a>优先队列</h3><p>定义：priority_queue&lt;Type, Container, Functional&gt;</p>
<p>他和<code>queue</code>不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队</p>
<ul>
<li>Type 就是数据类型</li>
<li>Container 就是容器类型（Container必须是用数组实现的容器，比如vector,deque等等，但不能用 list。STL里面默认用的是vector）</li>
<li>Functional 就是比较的方式。</li>
<li>当需要用自定义的数据类型时才需要传入这三个参数，使用基本数据类型时，只需要传入数据类型，默认是大顶堆。</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//升序队列</span></span><br><span class="line">priority_queue &lt;<span class="keyword">int</span>,<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;,greater&lt;<span class="keyword">int</span>&gt; &gt; q;</span><br><span class="line"><span class="comment">//降序队列</span></span><br><span class="line">priority_queue &lt;<span class="keyword">int</span>,<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;,less&lt;<span class="keyword">int</span>&gt; &gt;q;</span><br><span class="line"><span class="comment">//greater和less是std实现的两个仿函数（就是使一个类的使用看上去像一个函数。</span></span><br><span class="line"><span class="comment">//其实现就是类中实现一个operator()，这个类就有了类似函数的行为，就是一个仿函数类了）</span></span><br></pre></td></tr></table></figure>

<p>和队列基本操作相同:</p>
<ul>
<li>top 访问队头元素</li>
<li>empty 队列是否为空</li>
<li>size 返回队列内元素个数</li>
<li>push 插入元素到队尾 (并排序)</li>
<li>emplace 原地构造一个元素并插入队列</li>
<li>pop 弹出队头元素</li>
<li>swap 交换内容</li>
</ul>
<h3 id="栈"><a href="#栈" class="headerlink" title="栈"></a>栈</h3><h4 id="stack的定义"><a href="#stack的定义" class="headerlink" title="stack的定义"></a>stack的定义</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">typename</span>&gt; name;</span><br></pre></td></tr></table></figure>

<h4 id="stack的状态"><a href="#stack的状态" class="headerlink" title="stack的状态"></a>stack的状态</h4><p>empty():返回当前向量是否为空</p>
<p>size():返回当前向量元素个数</p>
<h4 id="stack元素的添加或删除"><a href="#stack元素的添加或删除" class="headerlink" title="stack元素的添加或删除"></a>stack元素的添加或删除</h4><p>push():向栈顶添加新元素</p>
<p>pop():弹出栈顶元素</p>
<h4 id="queue的访问-1"><a href="#queue的访问-1" class="headerlink" title="queue的访问"></a>queue的访问</h4><p>只能用top()来访问栈的栈顶进行操作</p>
<h3 id="set"><a href="#set" class="headerlink" title="set"></a>set</h3><p>STL set中有set_union(取两集合并集)、set_intersection(取两集合交集)、set_difference(取两集合差集)。</p>
<p>1、这几个函数的参数一样。<br>2、set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.end()))，前两个参数是集合x1的头尾，再依次是集合x2的头尾，最后一个参数就是将集合x1和集合x2取合集后存入集合x中。</p>
<h3 id="set-diference"><a href="#set-diference" class="headerlink" title="set_diference"></a>set_diference</h3><p>set_diference就是取集合差集用前面参数的集合减去后面参数的集合把结果存在最后一个vector里面</p>
<h2 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h2><p>#include &lt;stdio.h&gt;</p>
<p>fopen:打开一个文件</p>
<p>fclose:关闭一个文件</p>
<p>fwrite:写入数据</p>
<p>fread:读出数据</p>
<h3 id="保存数据到文件"><a href="#保存数据到文件" class="headerlink" title="保存数据到文件"></a>保存数据到文件</h3><ol>
<li><p>fopen：打开文件</p>
<p>filename是全路径</p>
<p>FILE *fopen(const char *filename,const char *mode);</p>
<p>mode:使用”wb”(w表示write，b表示binary)</p>
<p>返回值：文件指针 FILE*</p>
<p>示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">char</span>* filename = <span class="string">"c:/aaa.txt"</span>;</span><br><span class="line">FILE* fp = fopen(filename,<span class="string">"wb"</span>);</span><br><span class="line"><span class="keyword">if</span>(fp == <span class="literal">NULL</span>)&#123;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">"failed to open file!\n"</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ol>
<ol start="2">
<li><p>fwrite</p>
<p>size_t fwrite(const void *buf,size_t size,size_t count,FILE *stream);</p>
<p>参数：stream就是前面fopen的返回值</p>
<p>​         buf，要写入的数据</p>
<p>​         size，总是传1</p>
<p>​         count，字节数</p>
<p>返回值：字节数</p>
<p>示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> buf[] = <span class="string">"hello"</span>;</span><br><span class="line">fwrite(buf,<span class="number">1</span>,<span class="number">5</span>,fp);</span><br></pre></td></tr></table></figure>



</li>
</ol>
<ol start="3">
<li><p>fclose</p>
<p>int fclose(FILE* stream);</p>
<p>参数：stream就是前面fopen的返回值</p>
<p>示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fclose(fp);</span><br></pre></td></tr></table></figure>


</li>
</ol>
<h2 id="C-文件操作"><a href="#C-文件操作" class="headerlink" title="C++文件操作"></a>C++文件操作</h2><p>先写文件名路径（找不到则创建）：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="comment">//输入输出相关头文件</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">char</span>* ifile =<span class="string">"/Users/yangshucheng/Desktop/C++/fushi/fushi/in.txt"</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">char</span>* ofile =<span class="string">"/Users/yangshucheng/Desktop/C++/fushi/fushi/out.txt"</span>;</span><br></pre></td></tr></table></figure>

<p>打开读入文件：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">ifstream <span class="title">in</span><span class="params">(ifile)</span></span>;</span><br></pre></td></tr></table></figure>

<p>读入文件数据操作：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">in &gt;&gt; arr[i][j];<span class="comment">//将文件的数据输入到指定的数组当中</span></span><br></pre></td></tr></table></figure>

<p>打开输出文件：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">ofstream <span class="title">out</span><span class="params">(ofile)</span></span>;</span><br></pre></td></tr></table></figure>

<p>输出数据到文件：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">out &lt;&lt; x &lt;&lt;<span class="built_in">endl</span>;</span><br></pre></td></tr></table></figure>

<p>关闭打开文件：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">in.<span class="built_in">close</span>();</span><br><span class="line">out.<span class="built_in">close</span>();</span><br></pre></td></tr></table></figure>

<h3 id="详细解释"><a href="#详细解释" class="headerlink" title="详细解释"></a>详细解释</h3><h4 id="打开文件"><a href="#打开文件" class="headerlink" title="打开文件"></a><strong>打开文件</strong></h4><p>文件名可以给出绝对路径，也可以给相对路径。没有交代路径信息，就是在当前文件夹下找文件</p>
<p>在从文件读取信息或者向文件写入信息之前，必须先打开文件。<strong>ofstream</strong> 和 <strong>fstream</strong> 对象都可以用来打开文件进行写操作，如果只需要打开文件进行读操作，则使用 <strong>ifstream</strong> 对象。</p>
<p>下面是 open() 函数的标准语法，open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">open</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *filename, ios::openmode mode)</span></span>;</span><br></pre></td></tr></table></figure>

<p>在这里，<strong>open()</strong> 成员函数的第一参数指定要打开的文件的名称和位置，第二个参数定义文件被打开的模式。</p>
<table>
<thead>
<tr>
<th>模式标志</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>ios::app</td>
<td>追加模式。所有写入都追加到文件末尾。</td>
</tr>
<tr>
<td>ios::binary</td>
<td>以二进制文件格式打开文件，默认是文本打开形式</td>
</tr>
<tr>
<td>ios::ate</td>
<td>文件打开后定位到文件末尾。</td>
</tr>
<tr>
<td>ios::beg</td>
<td>定位到文件开头。</td>
</tr>
<tr>
<td>ios::in</td>
<td>打开文件用于读取。</td>
</tr>
<tr>
<td>ios::out</td>
<td>打开文件用于写入。</td>
</tr>
<tr>
<td>ios::trunc</td>
<td>如果该文件已经存在，其内容将在打开文件之前被截断，即把文件长度设为 0。</td>
</tr>
</tbody></table>
<p>您可以把以上两种或两种以上的模式结合使用。例如，如果您想要以写入模式打开文件，并希望截断文件，以防文件已存在，那么您可以使用下面的语法：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ofstream outfile;</span><br><span class="line">outfile.<span class="built_in">open</span>(<span class="string">"file.dat"</span>, ios::out | ios::trunc );</span><br></pre></td></tr></table></figure>

<p>类似地，您如果想要打开一个文件用于读写，可以使用下面的语法：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ifstream  afile;</span><br><span class="line">afile.<span class="built_in">open</span>(<span class="string">"file.dat"</span>, ios::out | ios::in );</span><br></pre></td></tr></table></figure>

<h4 id="关闭文件"><a href="#关闭文件" class="headerlink" title="关闭文件"></a>关闭文件</h4><p>当 C++ 程序终止时，它会自动关闭刷新所有流，释放所有分配的内存，并关闭所有打开的文件。但程序员应该养成一个好习惯，在程序终止前关闭所有打开的文件。</p>
<p>下面是 close() 函数的标准语法，close() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void close();</span><br></pre></td></tr></table></figure>

<h4 id="写入文件"><a href="#写入文件" class="headerlink" title="写入文件"></a>写入文件</h4><p>在 C++ 编程中，我们使用流插入运算符（ &lt;&lt; ）向文件写入信息，就像使用该运算符输出信息到屏幕上一样。唯一不同的是，在这里您使用的是 <strong>ofstream</strong> 或 <strong>fstream</strong> 对象，而不是 <strong>cout</strong> 对象。</p>
<h4 id="读取文件"><a href="#读取文件" class="headerlink" title="读取文件"></a>读取文件</h4><p>在 C++ 编程中，我们使用流提取运算符（ &gt;&gt; ）从文件读取信息，就像使用该运算符从键盘输入信息一样。唯一不同的是，在这里您使用的是 <strong>ifstream</strong> 或 <strong>fstream</strong> 对象，而不是 <strong>cin</strong> 对象。</p>
<h3 id="二进制文件读写"><a href="#二进制文件读写" class="headerlink" title="二进制文件读写"></a>二进制文件读写</h3><h4 id="二进制读文件"><a href="#二进制读文件" class="headerlink" title="二进制读文件"></a>二进制读文件</h4><p>ifstream 和 fstream的成员函数：</p>
<p><strong>istream&amp; read (char* s, long n);</strong></p>
<p>将文件读指针指向的地方的n个字节内容，读入到内存地址s，然后将文件读指针向后移动n字节 (以ios::in方式打开文件时，文件读指针开始指向文件开头) 。</p>
<h4 id="二进制写文件"><a href="#二进制写文件" class="headerlink" title="二进制写文件"></a>二进制写文件</h4><p>istream&amp; write (const char* s, long n);<br>将内存地址s处的n个字节内容，写入到文件中写指针指向的位置，然后将文件写指针向后移动n字节(以ios::out方式打开文件时，文件写指针开始指向文件开头, ios::app方式打开文件时，文件写指针开始指向文件尾部 ) 。</p>
<blockquote>
<p>从键盘输入几个学生的姓名的成绩，并以二进制文件形式保存</p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> &#123;</span></span><br><span class="line">		<span class="keyword">char</span> name[<span class="number">20</span>];</span><br><span class="line">		<span class="keyword">int</span> score;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Student s;</span><br><span class="line"><span class="comment">/**********************写入操作**********************************/</span></span><br><span class="line">		<span class="function">ofstream <span class="title">OutFile</span><span class="params">( <span class="string">"c:\\tmp\\students.dat"</span>,ios::out|ios::binary)</span></span>;</span><br><span class="line">		<span class="keyword">while</span>( <span class="built_in">cin</span> &gt;&gt; s.name &gt;&gt; s.score )</span><br><span class="line">				OutFile.<span class="built_in">write</span>( (<span class="keyword">char</span> * ) &amp; s, <span class="keyword">sizeof</span>( s) );<span class="comment">//写入到目的文件</span></span><br><span class="line">		OutFile.<span class="built_in">close</span>();</span><br><span class="line"> <span class="comment">/**********************读入操作**********************************/</span></span><br><span class="line">    <span class="function">ifstream <span class="title">inFile</span><span class="params">(<span class="string">"students.dat"</span>,ios::in | ios::binary )</span></span>;</span><br><span class="line">		<span class="keyword">if</span>(!inFile) &#123;</span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; <span class="string">"error"</span> &lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">				<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">while</span>( inFile.<span class="built_in">read</span>( (<span class="keyword">char</span>* ) &amp; s, <span class="keyword">sizeof</span>(s) ) ) &#123;</span><br><span class="line">				<span class="keyword">int</span> readedBytes = inFile.gcount(); <span class="comment">//看刚才读了多少字节</span></span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; s.name &lt;&lt; <span class="string">" "</span> &lt;&lt; s.score &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		inFile.<span class="built_in">close</span>();</span><br><span class="line"> <span class="comment">/**********************写入特定位置操作**********************************/</span></span><br><span class="line">		<span class="function">fstream <span class="title">iofile</span><span class="params">( <span class="string">"c:\\tmp\\students.dat"</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">									ios::in|ios::out|ios::binary)</span></span>;</span><br><span class="line">		<span class="keyword">if</span>( !iofile) &#123;</span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; <span class="string">"error"</span> ;</span><br><span class="line">				<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		iofile.seekp( <span class="number">2</span> * <span class="keyword">sizeof</span>(s),ios::beg); <span class="comment">//定位写指针到第三个记录</span></span><br><span class="line">		iofile.<span class="built_in">write</span>(<span class="string">"Mike"</span>,<span class="built_in">strlen</span>(<span class="string">"Mike"</span>)+<span class="number">1</span>);</span><br><span class="line">		iofile.seekg(<span class="number">0</span>,ios::beg); <span class="comment">//定位读指针到开头</span></span><br><span class="line">		<span class="keyword">while</span>( iofile.<span class="built_in">read</span>( (<span class="keyword">char</span>* ) &amp; s, <span class="keyword">sizeof</span>(s)) )</span><br><span class="line">				<span class="built_in">cout</span> &lt;&lt; s.name &lt;&lt; <span class="string">" "</span> &lt;&lt; s.score &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		iofile.<span class="built_in">close</span>();</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	输出：</span></span><br><span class="line"><span class="comment">	Tom 60</span></span><br><span class="line"><span class="comment">	Jack 80</span></span><br><span class="line"><span class="comment">	Mike 40</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="remove"><a href="#remove" class="headerlink" title="remove()"></a>remove()</h3><p>头文件: #include <cstdio> //C++</p>
<p>函数原型：int remove(const char * filename);</p>
<p>返回结果：如果成功返回 0，失败返回“EOF”（ -1）。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> *savePath = <span class="string">"/Users/yangshucheng/Desktop/111.txt"</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">remove</span>(savePath)==<span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span>&lt;&lt;<span class="string">"删除成功"</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span>&lt;&lt;<span class="string">"删除失败"</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="rename"><a href="#rename" class="headerlink" title="rename()"></a>rename()</h3><p>头文件: #include <cstdio> //C++</p>
<p>函数原型：int rename(char  * oldname, char* newname);</p>
<p>【参数】oldname为旧文件名，newname为新文件名。</p>
<p>【返回值】修改文件名成功则返回0，否则返回-1。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> *savePath = <span class="string">"/Users/yangshucheng/Desktop/11111.txt"</span>;</span><br><span class="line">    <span class="keyword">char</span> *toPath = <span class="string">"/Users/yangshucheng/Desktop/8888.txt"</span>;</span><br><span class="line">    <span class="keyword">if</span>(rename(savePath,toPath)==<span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span>&lt;&lt;<span class="string">"重命名成功"</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span>&lt;&lt;<span class="string">"重命名失败"</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/yangshucheng2020/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
          
            <a href="/yangshucheng2020/tags/C-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag"># C++基础知识</a>
          
            <a href="/yangshucheng2020/tags/%E5%88%B7%E9%A2%98%E5%BF%85%E5%A4%87/" rel="tag"># 刷题必备</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/yangshucheng2020/2020/03/27/%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E7%A8%8B%E5%BA%8F/" rel="next" title="算法设计程序">
                <i class="fa fa-chevron-left"></i> 算法设计程序
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/yangshucheng2020/2020/03/27/%E7%96%91%E9%9A%BE%E9%97%AE%E9%A2%98/" rel="prev" title="疑难问题">
                疑难问题 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://gitee.com/yangshucheng2020/blogimage/raw/master/uPic/海绵jojo.jpg"
                alt="羊书change" />
            
              <p class="site-author-name" itemprop="name">羊书change</p>
              <p class="site-description motion-element" itemprop="description">记录生活</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/yangshucheng2020/archives/%7C%7Carchive">
              
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/yangshucheng2020/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/yangshucheng2020/tags/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yangshucheng1219" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:ysc714@163.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#头文件"><span class="nav-number">1.</span> <span class="nav-text">头文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#scanf与printf"><span class="nav-number">2.</span> <span class="nav-text">scanf与printf</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#常见数据类型变量的scanf格式符"><span class="nav-number">2.1.</span> <span class="nav-text">常见数据类型变量的scanf格式符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常见的printf格式符"><span class="nav-number">2.2.</span> <span class="nav-text">常见的printf格式符</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#三种实用的输出格式"><span class="nav-number">2.2.1.</span> <span class="nav-text">三种实用的输出格式</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用getchar和putchar输入-输出字符"><span class="nav-number">3.</span> <span class="nav-text">使用getchar和putchar输入&#x2F;输出字符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#getline-lt-字符数组chs-gt-，-lt-读取字符的个数n-gt-，-lt-终止符-gt"><span class="nav-number">4.</span> <span class="nav-text">getline(&lt;字符数组chs&gt;，&lt;读取字符的个数n&gt;，&lt;终止符&gt;)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gets输入与puts输出"><span class="nav-number">5.</span> <span class="nav-text">gets输入与puts输出</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#gets"><span class="nav-number">5.0.1.</span> <span class="nav-text">gets</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#输入字符串特别提醒"><span class="nav-number">6.</span> <span class="nav-text">输入字符串特别提醒</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#结束符-0的ASCII码为0，即空字符NULL，占用一个字符位，因此开辟数组时必须保证字符数组的长度一定要比实际存储字符串的长度至少多1"><span class="nav-number">6.0.1.</span> <span class="nav-text">结束符\0的ASCII码为0，即空字符NULL，占用一个字符位，因此开辟数组时必须保证字符数组的长度一定要比实际存储字符串的长度至少多1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#如果不是使用scanf函数的-s格式或gets函数输入字符串（例如使用getchar），一定要在输入的每个字符串后面加入“-0”"><span class="nav-number">6.0.2.</span> <span class="nav-text">如果不是使用scanf函数的%s格式或gets函数输入字符串（例如使用getchar），一定要在输入的每个字符串后面加入“\0”</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用math函数"><span class="nav-number">7.</span> <span class="nav-text">常用math函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#fabs-double-x"><span class="nav-number">7.1.</span> <span class="nav-text">fabs(double x)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#floor-double-x-和ceil-double-x"><span class="nav-number">7.2.</span> <span class="nav-text">floor(double x)和ceil(double x)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pow-double-r-double-p"><span class="nav-number">7.3.</span> <span class="nav-text">pow(double r,double p)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sqrt-double-x"><span class="nav-number">7.4.</span> <span class="nav-text">sqrt(double x)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#log-double-x"><span class="nav-number">7.5.</span> <span class="nav-text">log(double x)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sin-double-x-、cos-double-x-、tan-double-x"><span class="nav-number">7.6.</span> <span class="nav-text">sin(double x)、cos(double x)、tan(double x)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#asin-double-x-、acos-double-x-、atan-double-x"><span class="nav-number">7.7.</span> <span class="nav-text">asin(double x)、acos(double x)、atan(double x)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#round-double-x"><span class="nav-number">7.8.</span> <span class="nav-text">round(double x)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#转义字符"><span class="nav-number">8.</span> <span class="nav-text">转义字符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#符号常量和const常量"><span class="nav-number">9.</span> <span class="nav-text">符号常量和const常量</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#符号常量："><span class="nav-number">9.0.1.</span> <span class="nav-text">符号常量：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#const常量"><span class="nav-number">9.0.2.</span> <span class="nav-text">const常量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#注意"><span class="nav-number">9.0.3.</span> <span class="nav-text">注意</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#typedef"><span class="nav-number">10.</span> <span class="nav-text">typedef</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#memeset——对数组中每一个元素赋相同的值"><span class="nav-number">11.</span> <span class="nav-text">memeset——对数组中每一个元素赋相同的值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#string-h头文件"><span class="nav-number">12.</span> <span class="nav-text">string.h头文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#strlen"><span class="nav-number">12.1.</span> <span class="nav-text">strlen()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#strcmp"><span class="nav-number">12.2.</span> <span class="nav-text">strcmp()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#strcpy"><span class="nav-number">12.3.</span> <span class="nav-text">strcpy()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#strcat"><span class="nav-number">12.4.</span> <span class="nav-text">strcat()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串-头文件"><span class="nav-number">13.</span> <span class="nav-text">字符串 头文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#cin与cout"><span class="nav-number">14.</span> <span class="nav-text">cin与cout</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#流操纵算子"><span class="nav-number">14.1.</span> <span class="nav-text">流操纵算子</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#指针变量"><span class="nav-number">15.</span> <span class="nav-text">指针变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#指针与数组"><span class="nav-number">15.1.</span> <span class="nav-text">指针与数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用指针变量作为函数参数"><span class="nav-number">15.2.</span> <span class="nav-text">使用指针变量作为函数参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#引用"><span class="nav-number">16.</span> <span class="nav-text">引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结构体"><span class="nav-number">17.</span> <span class="nav-text">结构体</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#访问结构体内的元素"><span class="nav-number">17.1.</span> <span class="nav-text">访问结构体内的元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构体的构造函数初始化"><span class="nav-number">17.2.</span> <span class="nav-text">结构体的构造函数初始化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#浮点数的比较"><span class="nav-number">18.</span> <span class="nav-text">浮点数的比较</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#STL介绍"><span class="nav-number">19.</span> <span class="nav-text">STL介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vector"><span class="nav-number">19.1.</span> <span class="nav-text">vector</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#vector的定义"><span class="nav-number">19.1.1.</span> <span class="nav-text">vector的定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vector的状态"><span class="nav-number">19.1.2.</span> <span class="nav-text">vector的状态</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vector尾部元素的添加或删除"><span class="nav-number">19.1.3.</span> <span class="nav-text">vector尾部元素的添加或删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vector元素的访问"><span class="nav-number">19.1.4.</span> <span class="nav-text">vector元素的访问</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vector元素的操作"><span class="nav-number">19.1.5.</span> <span class="nav-text">vector元素的操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vector迭代器操作"><span class="nav-number">19.1.6.</span> <span class="nav-text">vector迭代器操作</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#map"><span class="nav-number">19.2.</span> <span class="nav-text">map</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#map的定义"><span class="nav-number">19.2.1.</span> <span class="nav-text">map的定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map的状态"><span class="nav-number">19.2.2.</span> <span class="nav-text">map的状态</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map的添加与删除"><span class="nav-number">19.2.3.</span> <span class="nav-text">map的添加与删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map元素操作"><span class="nav-number">19.2.4.</span> <span class="nav-text">map元素操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map迭代器操作"><span class="nav-number">19.2.5.</span> <span class="nav-text">map迭代器操作</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列"><span class="nav-number">19.3.</span> <span class="nav-text">队列</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#queue的定义"><span class="nav-number">19.3.1.</span> <span class="nav-text">queue的定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#queue的状态"><span class="nav-number">19.3.2.</span> <span class="nav-text">queue的状态</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#queue尾部元素的添加或删除"><span class="nav-number">19.3.3.</span> <span class="nav-text">queue尾部元素的添加或删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#queue的访问"><span class="nav-number">19.3.4.</span> <span class="nav-text">queue的访问</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优先队列"><span class="nav-number">19.4.</span> <span class="nav-text">优先队列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#栈"><span class="nav-number">19.5.</span> <span class="nav-text">栈</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#stack的定义"><span class="nav-number">19.5.1.</span> <span class="nav-text">stack的定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#stack的状态"><span class="nav-number">19.5.2.</span> <span class="nav-text">stack的状态</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#stack元素的添加或删除"><span class="nav-number">19.5.3.</span> <span class="nav-text">stack元素的添加或删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#queue的访问-1"><span class="nav-number">19.5.4.</span> <span class="nav-text">queue的访问</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set"><span class="nav-number">19.6.</span> <span class="nav-text">set</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set-diference"><span class="nav-number">19.7.</span> <span class="nav-text">set_diference</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文件操作"><span class="nav-number">20.</span> <span class="nav-text">文件操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#保存数据到文件"><span class="nav-number">20.1.</span> <span class="nav-text">保存数据到文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#C-文件操作"><span class="nav-number">21.</span> <span class="nav-text">C++文件操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#详细解释"><span class="nav-number">21.1.</span> <span class="nav-text">详细解释</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#打开文件"><span class="nav-number">21.1.1.</span> <span class="nav-text">打开文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#关闭文件"><span class="nav-number">21.1.2.</span> <span class="nav-text">关闭文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#写入文件"><span class="nav-number">21.1.3.</span> <span class="nav-text">写入文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#读取文件"><span class="nav-number">21.1.4.</span> <span class="nav-text">读取文件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二进制文件读写"><span class="nav-number">21.2.</span> <span class="nav-text">二进制文件读写</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#二进制读文件"><span class="nav-number">21.2.1.</span> <span class="nav-text">二进制读文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#二进制写文件"><span class="nav-number">21.2.2.</span> <span class="nav-text">二进制写文件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#remove"><span class="nav-number">21.3.</span> <span class="nav-text">remove()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rename"><span class="nav-number">21.4.</span> <span class="nav-text">rename()</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">羊书change</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/yangshucheng2020/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/yangshucheng2020/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/yangshucheng2020/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/yangshucheng2020/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/yangshucheng2020/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/yangshucheng2020/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/yangshucheng2020/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/yangshucheng2020/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/yangshucheng2020/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/yangshucheng2020/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/yangshucheng2020/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
